package com.yj.ccs.abs.web.management.controller;

import com.yj.ccs.abs.boot.authorization.annotation.CurrentUser;
import com.yj.ccs.abs.boot.authorization.annotation.UserRole;
import com.yj.ccs.abs.boot.pagination.Page;
import com.yj.ccs.abs.common.dto.IdDto;
import com.yj.ccs.abs.common.enums.UserRoleType;
import com.yj.ccs.abs.common.model.BaseResult;
import com.yj.ccs.abs.web.management.dto.*;
import com.yj.ccs.abs.web.management.entity.*;
import com.yj.ccs.abs.web.management.service.ClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/16
 * Author     : Unbelievable9
 **/
@Api(tags = "Web-Client-Controller")
@RestController
@RequestMapping("/web/client")
public class ClientController {

    private final ClientService clientService;

    @Autowired
    public ClientController(ClientService clientService) {
        this.clientService = clientService;
    }

    @ApiOperation("获取企业客户列表")
    @GetMapping("/list")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> list(KeywordDto keywordDto, @CurrentUser User user) {
        Page<ClientPageItem> clientPage = clientService.getClientPage(keywordDto);

        return new ResponseEntity<>(BaseResult.success("获取企业客户列表成功").setData(clientPage), HttpStatus.OK);
    }

    @ApiOperation("获取母公司列表")
    @GetMapping("/list-parent")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> listParent(@Validated ListParentDto listParentDto, @CurrentUser User user) {
        List<ParentCompany> parentCompanyList = clientService.getParentCompanyList(listParentDto);

        return new ResponseEntity<>(BaseResult.success("获取母公司列表成功").setData(parentCompanyList), HttpStatus.OK);
    }

    @ApiOperation("创建企业客户")
    @PostMapping("/create")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> create(@RequestBody @Validated CreateClientDto createClientDto, @CurrentUser User user) throws IOException {
        Client client = clientService.getClientByName(createClientDto.getName());

        if (client != null) {
            return new ResponseEntity<>(BaseResult.error("企业客户公司名称已存在, 无法创建"), HttpStatus.OK);
        } else {
            String error = clientService.createClient(createClientDto);

            if (error != null) {
                return new ResponseEntity<>(BaseResult.error(error), HttpStatus.OK);
            } else {
                return new ResponseEntity<>(BaseResult.success("创建企业客户成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("获取企业客户详情")
    @GetMapping("/detail")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> detail(@Validated IdDto idDto, @CurrentUser User user) {
        Client client = clientService.getClientById(idDto.getId());

        if (client == null) {
            return new ResponseEntity<>(BaseResult.error("企业客户不存在"), HttpStatus.OK);
        } else {
            ClientDetail clientDetail = clientService.getClientDetail(client);

            return new ResponseEntity<>(BaseResult.success("获取企业客户详情成功").setData(clientDetail), HttpStatus.OK);
        }
    }

    @ApiOperation("编辑企业客户信息")
    @PostMapping("/update")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> update(@RequestBody @Validated UpdateClientDto updateClientDto, @CurrentUser User user) {
        Client client = clientService.getClientById(updateClientDto.getId());

        if (client == null) {
            return new ResponseEntity<>(BaseResult.error("企业客户不存在"), HttpStatus.OK);
        } else {
            String error = clientService.updateClient(updateClientDto);

            if (error != null) {
                return new ResponseEntity<>(BaseResult.error(error), HttpStatus.OK);
            } else {
                return new ResponseEntity<>(BaseResult.success("编辑企业客户信息成功"), HttpStatus.OK);
            }
        }
    }

    @ApiOperation("删除企业客户")
    @PostMapping("/delete")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> delete(@RequestBody @Validated IdListDto idListDto, @CurrentUser User user) {
        clientService.deleteClient(idListDto);

        return new ResponseEntity<>(BaseResult.success("删除企业客户成功"), HttpStatus.OK);
    }
}
